<?php
	include_once("system/main.php");
	include 'header.php';
	if (!isset($_GET['page'])) {
		$page=1;
	}
	else {	
		$page=$_GET['page']; 
	}
	$offset=10*($page-1);
	$param="?";
	?>
	<div id="browse"> <?php
	if ( isset( $_SESSION['username'] ) ) {
		if ( empty( $_GET['mode'] ) ) {
			$mode = 2;
		}
		if ($_GET['mode'] == 1) {
			$order = "ASC"; $mode=2;
			$param.="mode=1&";
		}
		else if ($_GET['mode'] == 2) { 
			$order = "DESC"; $mode=1;
			$param.="mode=2&";
		}
		if ( isset( $_GET['date']) ) {
			$res = $purevote->db_select_browse_sort("10", "created", "username", "pollname", "description", "created", "pollid", $offset, $order);
			$param.="date=yes&";
		}
		else if ( isset( $_GET['user'] ) ) {
			$res = $purevote->db_select_browse_sort("10", "username", "username", "pollname", "description", "created", "pollid", $offset, $order);
			$param.="user=yes&";
		}
		else if ( isset( $_GET['name'] ) ) {
			$res = $purevote->db_select_browse_sort("10", "pollname", "username", "pollname", "description", "created", "pollid", $offset, $order);
			$param.="name=yes&";
		}
		else {
			$res = $purevote->db_select_browse_sort("10", "username", "pollname", "description", "created", "username", "pollid", $offset, $order);
		}	
		if ($_GET['all']==1) {?> <span id="allvoted">Έχεις ψηφίσει σε όλες τις ψηφοφορίες!</span> <?php } ?>		
		<table id="tabl"><tr><td class='headname'><?php echo "<a href='./browse.php?name=yes&mode=".$mode."'>"; ?>Όνομα ψηφοφορίας</a></td><td class='headuser'><?php echo "<a href='./browse.php?user=yes&mode=".$mode."'>"; ?>Από</a></td><td class='headwhen'><?php echo "<a href='./browse.php?date=yes&mode=".$mode."'>"?>Ξεκίνησε στις</a></td></tr>
		<?php $counter = 0;
		$counter2 = 12; 
		foreach ( $res as $poll ) { ?>
			        <tr>
					<td><?php echo "<span class='pollname' id=\"".$counter."\">".$poll['pollname']."</span>"; ?></td>
					<td><?php echo "<span class='username'>".$poll['username']."</span>"; ?></td>
					<td><?php echo $poll['created']; ?></td>			
				</tr>
				<tr><td><div  <?php echo "id=\"".$counter2."\""?> class="showdet" style="display: none"></div></td></tr><?php
			$pollid=$poll['pollid'];
			$userid=$_SESSION['userid'];
			$res2 = $purevote->db_get_ifvoted("$pollid", "$userid", "userid");
			foreach ( $res2 as $poll2 ) {
				$voted = 1;
			}
			if ($voted == 1) {
				echo "<script>
				var buttons = $( '#$counter' );
				buttons.click(function() {
				$('#$counter2').html(\"<div> ".$poll['description']."</div><div>Έχεις ήδη ψηφίσει σε αυτή τη ψηφοφορία.<br/>Δες <a href='./vote.php?id=".$poll['pollid']."'>εδώ</a> τα αποτελέσματα.</div>\");
				$('#$counter2').toggle();
				 });
				</script>";
				$voted=0;
			}
			else {	
				echo "<script>
				var buttons = $( '#$counter' );
				buttons.click(function() {
				$('#$counter2').html(\"<div> ".$poll['description']."</div><div><a href='./vote.php?id=".$poll['pollid']."'>Ψήφισε!</a></div>\");
				$('#$counter2').toggle();
				 });
				</script>";
			}
			$counter++;
			$counter2++;
		}?>
		</table>
		<?php
		if (($prevpage=$page-1)!=0) {
			echo "<a id=\"prev\"href=\"./browse.php".$param."page=".$prevpage."\"> Προηγούμενη </a>";			
		}
		$res=mysql_query(
			"SELECT pollid FROM polls;"
		);
		$totalpolls=mysql_num_rows($res);
		if ($totalpolls>10*$page) {
			$nextpage=$page+1;
			echo "<a id=\"next\"href=\"./browse.php".$param."page=".$nextpage."\"> Επόμενη </a>";	
		}
	}
	else { ?> 
			<p>Πρέπει να είσαι συνδεδεμένος για να μπορείς να ψηφίσεις! </p>
			<p>Αν δεν έχεις λογαριασμό, δημιούργησε έναν  <a href='register.php'>εδώ</a>.</p>
	 <?php }	

	?>
	</div>
	<?php

	include 'footer.php';
?>
